package unconditional;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.URI;

public class SQING_ERRCHeader {

    public static void readAndWriteJson(String hdfsUri, String outputPath, String localJsonPath) {
        Configuration conf = new Configuration();
        FileSystem fs = null;
        BufferedReader br = null;
        FileWriter fileWriter = null;

        try {
            fs = FileSystem.get(new URI(hdfsUri), conf);
            Path path = new Path(outputPath);

            br = new BufferedReader(new InputStreamReader(fs.open(path)));

            ObjectMapper mapper = new ObjectMapper();
            ArrayNode jsonArray = mapper.createArrayNode();

            String line;

            while ((line = br.readLine()) != null) {
                // 分割每一行为字段，这里假设使用制表符('\t')作为分隔符
                String[] fields = line.split("\t");

                // 确保至少有两列数据
                if (fields.length >= 2) {
                    ObjectNode jsonObject = mapper.createObjectNode();

                    // 假设第一列是"qingdao_id"，第二列是"count"，根据实际情况调整
                    jsonObject.put("qingdao_id", fields[0].trim()); // 使用trim()去除前后空白
                    jsonObject.put("count", fields[1].trim());      // 同上

                    // 将JSON对象添加到数组中
                    jsonArray.add(jsonObject);
                }
            }

// 将最终的JSON数组转换为字符串（可选）
            String jsonOutput = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonArray);
            System.out.println(jsonOutput);

            fileWriter = new FileWriter(localJsonPath);
            mapper.writerWithDefaultPrettyPrinter().writeValue(fileWriter, jsonArray);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeStream(br);
            IOUtils.closeStream(fileWriter);
        }
    }

    public static void main(String[] args) {
        String hdfsUri = "hdfs://192.168.43.129:8020";
        String outputPath = "/output/output_20240617163327/part-r-00000";
//        String localJsonPath = "hot_item.json";
        String localJsonPath = "src/main/resources/html/item4.json";

        readAndWriteJson(hdfsUri, outputPath, localJsonPath);
    }
}
